package com.xueyu.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.*;
import com.xueyu.domain.Orderapply;
import com.xueyu.domain.Orderdetail;
import com.xueyu.domain.Products.ProductsBean;
import com.xueyu.domain.SupplierBean;
import com.xueyu.domain.UpdateRequest;
import com.xueyu.domain.drug.drug;
import com.xueyu.domain.drugs.drugsBean;
import com.xueyu.domain.goods.Details;
import com.xueyu.domain.goods.OrderRequest;
import com.xueyu.domain.goods.Orderapplydetail;
import com.xueyu.domain.goods.Product;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/Details")
public class DetailsController {
    @Autowired
    DetailsDao DetailsDao;
    @Autowired
    OrdDao OrdDao;
    @Autowired
    DrugDao DrugDao;
    @Autowired
    drugsDao drugsDao;
    @Autowired
    OrderapplyDao OrderapplyDao;
    @Autowired
    SupplierDao SupplierDao;
    @Autowired
    ProductDao ProductDao;
    @Autowired
    OrderdetailDao OrderdetailDao;
    @Autowired
    ProductsDao ProductsDao;


    @RequestMapping("fp")
    public ResultBean fp() {
        ResultBean resultBean = new ResultBean();
        List<drugsBean> f =  drugsDao.selectList(null);
        QueryWrapper<SupplierBean> queryWrapper = new QueryWrapper<>();
        // 设置查询条件
        queryWrapper.eq("suppliers", 0);  // 根据字段相等进行查询
        List<SupplierBean> g =  SupplierDao.selectList(queryWrapper);
        resultBean.put("f",f);
        resultBean.put("g",g);
        return resultBean;
    }



    @RequestMapping("update")
    public ResultBean update(@RequestBody UpdateRequest request) {
        ResultBean resultBean = new ResultBean();
        List<Object> list = request.getList();  // 获取list字段
        // 存储要更新的药品id和对应的数量
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String currentDate = dateFormat.format(new Date());
        Random random = new Random();
        int randomNum = random.nextInt(900000000) + 1000000000; // 生成10位随机数
        String orderNumber = currentDate + randomNum; //订单号
        Orderapply  orderapply  = new Orderapply();
        orderapply.setOrderno(orderNumber);
        Map<Integer, Integer> prodCountsMap = new HashMap<>();
        for (Object obj : list) {
            Map<String, Object> item = (Map<String, Object>) obj;
            int id = (int) item.get("DrugID"); // 获取id字段的值
            int Prodcount = (int) item.get("Prodcount"); // 获取drugName字段的值
            QueryWrapper<Details> queryWrapper = Wrappers.query();
            queryWrapper.eq("prodid", id);
            List<Details> ProductsList = DetailsDao.selectList(queryWrapper);
            for (Details details : ProductsList){
                int difference = details.getQuantity() - Prodcount;
                prodCountsMap.put(id, difference);
                orderapply.setOrdertype(Integer.valueOf(String.valueOf(id)));
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String currentTime2 = sdf.format(date);
                orderapply.setOrderdate(currentTime2);
                orderapply.setPersonid(request.getPersonId());
                orderapply.setStyle(request.getDrugM());
                orderapply.setC(String.valueOf(Prodcount));
            }
            if (!prodCountsMap.isEmpty()) {
                List<Integer> prodidssList = new ArrayList<>(prodCountsMap.keySet());
                List<Integer> prodcountsList = new ArrayList<>(prodCountsMap.values());
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String currentTime2 = sdf.format(date);
                OrderapplyDao.insert(orderapply);
                DetailsDao.updates(prodidssList, prodcountsList,currentTime2);
            }
        }
        return resultBean;
    }


    @RequestMapping("updates")
    public ResultBean updates(@RequestBody UpdateRequest request) {
        ResultBean resultBean = new ResultBean();
        List<Object> list = request.getList();  // 获取list字段
        // 存储要更新的药品id和对应的数量
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String currentDate = dateFormat.format(new Date());
        Random random = new Random();
        int randomNum = random.nextInt(900000000) + 1000000000; // 生成10位随机数
        String orderNumber = currentDate + randomNum; //订单号
        Orderdetail orderapply  = new Orderdetail();
        orderapply.setOrderno(orderNumber);
        Map<Integer, Integer> prodCountsMap = new HashMap<>();
        for (Object obj : list) {
            Map<String, Object> item = (Map<String, Object>) obj;
            int id = (int) item.get("DrugID"); // 获取id字段的值
            int Prodcount = (int) item.get("Prodcount"); // 获取drugName字段的值

            QueryWrapper<Details> queryWrapper = Wrappers.query();
            queryWrapper.eq("itemid", id);
            List<Details> ProductsList = DetailsDao.selectList(queryWrapper);
            for (Details details : ProductsList){
                int difference = details.getQuantity() - Prodcount;
                prodCountsMap.put(id, difference);
                orderapply.setProdid(String.valueOf(id));
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String currentTime2 = sdf.format(date);
                orderapply.setOrderDate(currentTime2);
                orderapply.setPersonId(request.getPersonId());
                orderapply.setBz(request.getDrugM());
                orderapply.setC(String.valueOf(Prodcount));
            }
            if (!prodCountsMap.isEmpty()) {
                List<Integer> prodidssList = new ArrayList<>(prodCountsMap.keySet());
                List<Integer> prodcountsList = new ArrayList<>(prodCountsMap.values());
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String currentTime2 = sdf.format(date);
                OrderdetailDao.insert(orderapply);
                DetailsDao.updates1(prodidssList, prodcountsList,currentTime2);
            }
        }
        return resultBean;
    }

    @RequestMapping("/sy")
    public ResultBean sy() {
        ResultBean resultBean = new ResultBean();
        List<String> orderNoList = DetailsDao.getList();
        String orderNos = "'" + String.join("','", orderNoList) + "'";
        List<Details> orderDetails = DetailsDao.selectOrderDetailWithDrugsByOrderNoList(orderNos);
        resultBean.put("list", orderDetails);
        return resultBean;
    }

    @RequestMapping("/sy2")
    public ResultBean getOrder() {
        ResultBean resultBean = new ResultBean();
        List<String> orderNoList = DetailsDao.getList2();
        String orderNos = "'" + String.join("','", orderNoList) + "'";
        List<Details> orderDetails = DetailsDao.selectOrderDetailWithDrugsByOrderNoList2(orderNos);
        resultBean.put("list",orderDetails);
        return resultBean;
    }


    @RequestMapping("/list")
    public ResultBean getOrderDetailsByOrderNoList(@RequestBody Map map) {
        ResultBean resultBean = new ResultBean();
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        String orderno =(String) map.get("orderno");
        String ord =(String) map.get("ord");
        String name =(String) map.get("name");
        Page<Product> page = new Page<>(current, size);
        List<String> orderNoList = DetailsDao.getOrderNoList(page,orderno,ord,name);
        String orderNos = "'" + String.join("','", orderNoList) + "'";
        List<Details> orderDetails = DetailsDao.selectOrderDetailWithDrugsByOrderNoList(orderNos);
        resultBean.put("page",page);
        resultBean.put("list",orderDetails);
        return resultBean;
    }





    @RequestMapping("/list2")
    public ResultBean getOrderDetailsByOrderNoList2(@RequestBody Map map) {
        ResultBean resultBean = new ResultBean();
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        String orderno =(String) map.get("orderno");
        String ord =(String) map.get("ord");
        String name =(String) map.get("name");
        Page<Product> page = new Page<>(current, size);
        List<String> orderNoList = DetailsDao.getOrderNoList2(page,orderno,ord,name);
        String orderNos = "'" + String.join("','", orderNoList) + "'";
        List<Details> orderDetails = DetailsDao.selectOrderDetailWithDrugsByOrderNoList2(orderNos);
        resultBean.put("page",page);
        resultBean.put("list",orderDetails);
        return resultBean;
    }












    @RequestMapping("/get")
    public ResultBean gets(@RequestBody OrderRequest request) {
        ResultBean resultBean = new ResultBean();
        List<Map<String, String>> ordList = request.getOrd();
        List<Map<String, Object>> res = new ArrayList<>(); // 修改为 Map<String, Object> 类型的列表
        for (Map<String, String> ord : ordList) {
            String drug = ord.get("drug");
            String quantity = ord.get("quantity");
            QueryWrapper<drug> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("DrugID", drug);
            List<drug> page = DrugDao.selectList(queryWrapper);
           //遍历药品列表
            for (drug Drug : page) {
                // 获取药品的分类id
                Long categoryId = Long.valueOf(Drug.getDrugF());
                Long categoryIds = Long.valueOf(Drug.getSupplier());
                // 根据分类id查询对应的分类信息
                drugsBean category = drugsDao.selectById(categoryId);
                SupplierBean category2 = SupplierDao.selectById(categoryIds);
                // 如果分类存在，则将分类名称设置到药品表中
                if (category != null) {
                    Drug.setP(category.getDrugNames());
                }
                if (category2 != null) {
                    Drug.setG(category2.getSupplierName());
                }
            }
            for (drug d : page) {
                // 根据需要修改对应的属性值
                d.setPrice(d.getPrice() * 2);
                d.setProdcount(Integer.parseInt(quantity));
                if(d.getProdcount()>0) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("Prodcount", d.getProdcount());//数量
                    map.put("DrugID",d.getDrugID());//药品id
                    map.put("DrugName", d.getDrugName());//药品名称
                    map.put("Price", d.getPrice());//单价
                    map.put("DrugPic", d.getDrugPic());//图片
                    map.put("DrugF", d.getP());//分类
                    map.put("style", d.getStyle());//单位
                    map.put("supplier", d.getG());//供应商
                    map.put("DrugP", d.getDrugP());//批
                    map.put("DrugM", d.getDrugM());//描
                    map.put("X", d.getPrice()  * d.getProdcount());//小记
                    res.add(map);
                }
            }
        }
        resultBean.put("page", res); // 将新列表放入 resultBean 中
        return resultBean;
    }




    @RequestMapping("/gets")
    public ResultBean get(@RequestBody OrderRequest request) {
        ResultBean resultBean = new ResultBean();
        List<Map<String, String>> ordList = request.getOrd();
        List<Map<String, Object>> res = new ArrayList<>(); // 修改为 Map<String, Object> 类型的列表
        for (Map<String, String> ord : ordList) {
            String drug = ord.get("drug");
            String quantity = ord.get("quantity");
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("prodId", drug);
            List<Product> page = ProductDao.selectList(queryWrapper);

            //遍历药品列表
            for (Product Drug : page) {
                // 获取药品的分类id
                Long categoryId = Long.valueOf(Drug.getStyle());
                Long categoryIds = Long.valueOf(Drug.getSupplierid());
                // 根据分类id查询对应的分类信息
                ProductsBean category = ProductsDao.selectById(categoryId);
                SupplierBean category2 = SupplierDao.selectById(categoryIds);
                // 如果分类存在，则将分类名称设置到药品表中
                if (category != null) {
                    Drug.setP(category.getProductName());
                }
                if (category2 != null) {
                    Drug.setG(category2.getSupplierName());
                }
            }
            for (Product d : page) {
                // 根据需要修改对应的属性值
                d.setPrice(d.getPrice() * 2);
                d.setProdcount(Integer.parseInt(quantity));
                if(d.getProdcount()>0) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("Prodcount", d.getProdcount());//数量
                    map.put("DrugID",d.getProdid());//药品id
                    map.put("DrugName", d.getProdname());//药品名称
                    map.put("Price", d.getPrice());//单价
                    map.put("DrugPic", d.getBrand());//图片
                    map.put("DrugF", d.getP());//分类
                    map.put("style", d.getUnitname());//单位
                    map.put("supplier", d.getG());//供应商
                    map.put("DrugM", d.getDrugM());//描
                    map.put("X", d.getPrice()  * d.getProdcount());//小记
                    res.add(map);
                }
            }
        }
        resultBean.put("page", res); // 将新列表放入 resultBean 中
        return resultBean;
    }




}










